<?php


namespace app\wechat\controller;


use app\BaseController;
use app\common\model\Order;
use app\common\model\Finance;
use app\common\model\User;
use think\cache\driver\Redis;
use think\facade\Cache;
use think\facade\Db;
use think\exception\ValidateException;
class Student extends AuthBase
{
    protected $noLogin = ['index'];
    public function index()
    {
        $order=Db::name('order')
            ->alias('o')
            ->leftJoin('user u','u.id=o.student_id')
            ->where('o.student_id',$this->userId)
            ->field('o.id as order_id,u.head_img,u.nickname,u.id as user_id,o.price,o.yet_price,o.status,o.name,o.order_no,
            o.major,o.school_level,o.information,o.software_ask_one,o.software_ask_two,o.software_ask_three,o.middle_date,o.end_date')
            ->find();
        $order['middle_date']=date('Y-m-d',$order['middle_date']);
        $order['end_date']=!empty($order['end_date'])?date('Y-m-d',$order['end_date']):'';
        return  show(1,'请求成功',$order);
    }
    /**
     * 消息列表
     */
    public function message_index(){ 
        $student=[];
        $teacher=[];
        $res=1;//1显示发送2显示上次修改中
        $order=Db::name('order')->where('student_id',$this->userId)->find();
        $one=Db::name('message')
            ->where(['order_id'=>$order['id'],'type'=>1])
            ->order('id DESC')->limit(1)->find();
        $student=json_decode($one['content'],true);
        $two=Db::name('message')
            ->where('status',2)
            ->where(['order_id'=>$order['id'],'type'=>2])
            ->order('id DESC')->limit(1)->find();
        if(!$two){
            $res=2;
        }else{
            $teacher=json_decode($two['content'],true);
        }
        if($two){
            if($one['status']!=$two['status']){
                $res=2;
            }
        }
        return show(1,'',['student'=>$student,'teacher'=>$teacher,'is_res'=>$res]);
    }
    /**
     * 修改消息
     */
    public function return_message(){
        $data=input("post.");
        $order=Db::name('order')
            ->where('student_id',$this->userId)
            ->find();
        if($order['status']==3){
            $status=2;
        }elseif($order['status']==4){
            $status=3;
        }elseif($order['status']==5){
            $status=4;
        }elseif($order['status']==6){
            $status=5;
        }elseif($order['status']==7){
            $status=6;
        }elseif($order['status']==8){
            $status=7;
        }
        //判断是否发过消息
        $res=Db::name('message')
            ->where(['order_id'=>$order['id'],'order_status'=>$status,'student_id'=>$this->userId])
            ->count();
        if($res){
            return show(0,'您已经提交过消息了，请等待老师回复');
        }
        $result['teacher_id']=$order['teacher_id'];
        $result['student_id']=$this->userId;
        $result['content']=json_encode($data['content']);
        $result['order_id']=$order['id'];
        $result['status']=1;
        $result['order_status']= $status;
        $result['type']=1;
        $result['create_time']=time();
        $result['update_time']=time();
        $result['count']=count($data['content']);
        $res=Db::name('message')
            ->insert($result);
        if($res){
            return show(1,'发送成功');
        }
        return show(0,'发送失败');
    }
}